package com.youzhi.backend.dto;

import com.youzhi.backend.exception.EcpEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
@ApiModel(value = "响应实体", description = "服务端返回的响应实体")
public class Result<T> {

    @ApiModelProperty("状态码")
    private Integer code = 200;

    @ApiModelProperty("返回信息")
    private String msg = "success";

    @ApiModelProperty("返回数据对象")
    private T data;


    public static <T> Result<T> ok(T t) {
        Result<T> result = new Result<T>();
        result.setData(t);
        return result;
    }


    public static Result<Map> ok(String key, Object value) {
        Map<String, Object> map = new HashMap<>();
        map.put(key, value);
        return Result.ok(map);
    }

    public static <T> Result<T> ok() {
        return new Result<>();
    }


    public static Result error(int code, String msg) {
        Result result = new Result();
        result.code = code;
        result.msg = msg;
        return result;
    }


    public static Result error(EcpEnum ecpEnum) {
        Result result = new Result();
        result.code = ecpEnum.code;
        result.msg = ecpEnum.msg;
        return result;
    }


}
